This is the first version of the plot. There is a lot to work on here. Both axes and legend labels seem confusing. X-axis scale is also not complete. Also, it is hard to see any patterns without using facet_wrap and sorting the values.
The revised version looks much better. You can see interesting patterns such as Spain’s scores. The colors are terrible, though. Also, it’d be nice to see the grand mean to have a general reference category.
---
title: "Moral Values Across Countries"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
source_code: embed
---
```{r setup, include=FALSE}
# global options
knitr::opts_chunk$set(echo = FALSE,
tidy = TRUE,
cache = FALSE,
message = FALSE,
error = FALSE,
warning = FALSE)
# packages
library(flexdashboard)
library(here)
library(rio)
library(tidyverse)
library(magrittr)
library(lme4)
library(lmerTest)
library(colorBlindness)
library(dotwhisker)
library(tidytext)
theme_set(theme_minimal()) # set theme
options(scipen=999) # remove scientific notation
```
```{r wrangling, include = FALSE}
# import data
df <- import(here("data", "ALL_MFQ30.csv"), # moral values, countries, & sex
setclass = "tbl_df") %>%
janitor::clean_names()
df_c <- import(here("data", "Data_S1_sec.csv"), # country-level variables
setclass = "tbl_df") %>%
janitor::clean_names()
# data wrangling
df %<>%
drop_na() %>%
mutate(
across(where(is.double), as.numeric),
across(where(is.character), as.factor),
sex = recode(sex,
`1` = "Male",
`0` = "Female",
.default = NA_character_),
indiv = rowMeans(
select(., harm_avg, fairness_avg) # individualizing moral foundations
),
bind = rowMeans(
select(., ingroup_avg:purity_avg) # binding moral foundations
)
)
# check data structure and variables
str(df)
# descriptive statistics by country
c_desc <-
df %>%
pivot_longer(cols = c(indiv, bind),
names_to = "vars",
values_to = "val"
) %>%
select(country, vars, val) %>%
group_by(country, vars) %>%
summarise(mean = mean(val, na.rm = TRUE),
sd = sd(val, na.rm = TRUE),
min = min(val, na.rm = TRUE),
max = max(val, na.rm = TRUE),
.groups = "drop"
) %>%
mutate(vars = fct_recode(vars,
Individualizing = "indiv",
Binding = "bind"
)
)
# descriptive statistics by country and sex
c_s_desc <-
df %>%
filter(country != "Poland") %>% # Poland has missing data in sex.
pivot_longer(cols = c(indiv, bind),
names_to = "vars",
values_to = "val"
) %>%
group_by(country, sex, vars) %>%
summarise(mean = mean(val, na.rm = TRUE),
sd = sd(val, na.rm = TRUE),
min = min(val, na.rm = TRUE),
max = max(val, na.rm = TRUE),
.groups = "drop"
) %>%
mutate(vars = fct_recode(vars,
Individualizing = "indiv",
Binding = "bind"
)
)
```
# Values X Country
Sidebar {.sidebar}
-----------------------------------------------------------------------
**Data**
Data used in this dashboard come from the second study of XX. It's a publicly available dataset, which can be downloaded from XX.
19 countries.
**Summary of the Visualizations**
The first set of plots (on this page) represents the average scores for individualizing and binding foundations across countries. Next, building on this plot, I add the sex variable into the mix.
The second set of plots visualize the effect of sex and several country-level variables on the aforementioned foundations.
*Country-level variables*
a
a
a
a
a
a
Column {data-width=600}
-----------------------------------------------------------------------
### Final Version
```{r}
gmeans <-
c_desc %>%
group_by(vars) %>%
summarise(m = mean(mean))
c_desc %>%
ggplot() +
geom_vline(data = gmeans,
aes(xintercept = m),
linetype = 2,
alpha = .6) +
geom_col(
aes(mean, reorder_within(country, mean, vars),
fill = country,
alpha = .9
)
) +
scale_y_reordered() +
scale_x_continuous(expand = c(0, 0)
) +
facet_wrap(~vars,
scales = "free_y",
ncol = 2) +
theme(
plot.title.position = "plot",
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
legend.position = "none",
axis.text.y = element_text(color = "black",
size = 11),
axis.text.x = element_text(color = "black",
size = 9),
axis.title = element_blank()
) +
labs(
title = "Endorsement of Individualizing and Binding Moral Values Across Countries",
caption = "Vertical lines represent the average of all countries."
)
```
Column {data-width=400}
-----------------------------------------------------------------------
### Initial version
```{r}
c_desc %>%
ggplot() +
geom_col(
aes(mean, country, fill = vars),
position = "dodge"
) +
theme(
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank()
) +
scale_x_continuous(expand = c(0, 0))
```
> This is the first version of the plot. There is a lot to work on here. Both axes and legend labels seem confusing. X-axis scale is also not complete. Also, it is hard to see any patterns without using facet_wrap and sorting the values.
### Revised version
```{r}
c_desc %>%
ggplot() +
geom_col(
aes(mean, reorder_within(country, mean, vars)
)
) +
scale_y_reordered() +
scale_x_continuous(expand = c(0, 0)
) +
facet_wrap(~vars,
scales = "free_y",
ncol = 2) +
theme(
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
axis.text.y = element_text(color = "black",
size = 11),
axis.text.x = element_text(color = "black",
size = 9),
axis.title = element_blank()
) +
labs(
title = "Endorsement of Individualizing and Binding Moral Values Across Countries"
)
```
> The revised version looks much better. You can see interesting patterns such as Spain's scores. The colors are terrible, though. Also, it'd be nice to see the grand mean to have a general reference category.
# Values X Country X Sex
Column {data-width=600}
-----------------------------------------------------------------------
### Final Version
```{r}
```
Column {data-width=400}
-----------------------------------------------------------------------
### Initial version
```{r}
```
### Revised version
```{r}
```
# Predictors of values
Sidebar {.sidebar}
-----------------------------------------------------------------------
Column {data-width=600}
-----------------------------------------------------------------------
### Final Version
```{r}
```
Column {data-width=400}
-----------------------------------------------------------------------
### Initial version
```{r}
```
### Revised version
```{r}
```
# Predictors of values for each country
Column {data-width=600}
-----------------------------------------------------------------------
### Final Version
```{r}
```
Column {data-width=400}
-----------------------------------------------------------------------
### Initial version
```{r}
```
### Revised version
```{r}
```
# Bonus plots
Sidebar {.sidebar}
-----------------------------------------------------------------------
Column {data-width=600}
-----------------------------------------------------------------------
### Final Version
```{r}
```
Column {data-width=400}
-----------------------------------------------------------------------
### Initial version
```{r}
```
### Revised version
```{r}
```